/**
 * 仓库
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.repository;


import com.sxf.crm.entity.Clue;
import com.sxf.crm.enums.CluePriority;
import com.sxf.crm.enums.ClueSource;
import com.sxf.crm.enums.ClueStatus;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ClueRepository extends JpaRepository<Clue, Long> {
    
    @Query("SELECT c FROM Clue c WHERE " +
           "(:name IS NULL OR :name = '' OR c.name LIKE %:name%) AND " +
           "(:source IS NULL OR c.source = :source) AND " +
           "(:status IS NULL OR c.status = :status) AND " +
           "(:priority IS NULL OR c.priority = :priority)")
    Page<Clue> findByConditions(@Param("name") String name,
                                @Param("source") ClueSource source,
                                @Param("status") ClueStatus status,
                                @Param("priority") CluePriority priority,
                                Pageable pageable);
    
    List<Clue> findByOwnerId(Long ownerId);
    
    List<Clue> findByOwnerIdIsNull();
    
    List<Clue> findByRecycledTrue();
    
    @Query("SELECT c FROM Clue c WHERE c.lastFollowUpTime IS NULL OR c.lastFollowUpTime < :cutoffTime")
    List<Clue> findStaleClues(@Param("cutoffTime") java.time.LocalDateTime cutoffTime);
    
    // 统计方法
    long countByStatus(ClueStatus status);
    long countByStatusNotIn(List<ClueStatus> statuses);
} 